'use strict';
/**
 * @ngdoc function
 * @name sbAdminApp.controller:MainCtrl
 * @description
 * # MainCtrl
 * Controller of the sbAdminApp
 */
angular.module('sbAdminApp')
    .controller('interCtrl', function ($scope, $log, $state, $modal, $stateParams, interfaceService) {
        //初始化作用域变量
        $scope.lists = [];
        $scope.totalItems = 0;
        $scope.currentPage = 1;
        $scope.itempersize = 5;
        $scope.treedata = [];
        $scope.my_tree  = {};


        //分页查询方法
        $scope.pageQuery = function (groupid, currentPage, itempersize) {
            interfaceService.pageQuery(groupid, currentPage, itempersize).then(function (dataResponse) {
                $scope.lists = dataResponse.data['result'];
                $scope.totalItems = dataResponse.data['page'].totalRecord;
                $scope.group = dataResponse.data['group'];

            });
        }
        $scope.my_tree_handler = function (branch) {
            var _ref;
            $scope.output = "You selected: " + branch.label;
            if ((_ref = branch.data) != null ? _ref.description : void 0) {
                return $scope.output += '(' + branch.data.description + ')';
            }
        };

        var tree_selected = function (branch) {
        	$scope.pageQuery(branch.data.id, $scope.currentPage, $scope.itempersize);
            $log.info(branch)
        };

        //切换分页事件时触发方法
        $scope.pageChanged = function (groupid) {
            $scope.pageQuery(groupid, $scope.currentPage, $scope.itempersize);
        };
        //添加接口
        $scope.addInterface = function (groupid,intergroupid, interfaceId,flag) {
            var modalInstance = $modal.open({
                templateUrl: '/views/service/interface/addinterface.html',
                controller: 'interAddCardCtrl',
                resolve: {
                    "groupid": function () {
                        return angular.copy(groupid);
                    },
                    "intergroupid": function () {
                        return angular.copy(intergroupid);
                    },
                    "interfaceId": function () {
                        return angular.copy(interfaceId);
                    },
                    "flag": function () {
                        return angular.copy(flag);
                    }
                }

            });
            modalInstance.result.then(function () {
                $scope.pageQuery(groupid, $scope.currentPage, $scope.itempersize);
            }, function () {
            });
        }

        //删除
        $scope.deleteInter = function (id, groupid) {
            if (!confirm("你确定要删除吗? 删除后数据将无法恢复!")) {
                return false;
            }
            interfaceService.deleteInter(id).then(function (dataResponse) {
                $scope.pageQuery(groupid, $scope.currentPage, $scope.itempersize);
            });
        }

        $scope.redirectThreshold = function (interfaceId, interfaceName) {
            var modalInstance = $modal.open({
                templateUrl: '/views/service/warn/addwarnthreshold.html',
                controller: 'interCardCtrl',
                resolve: {
                    "interfaceId": function () {
                        return angular.copy(interfaceId);
                    },
                    "interfaceName": function () {
                        return angular.copy(interfaceName);
                    }
                }

            });
            modalInstance.result.then(function () {
                $state.go("home.service.warnthreshold", {}, {reload: true});
            }, function () {
            });
        }
        //上线通知
        $scope.redirectNotify = function (interfaceId) {
            $state.go("home.service.warnfindmember", {"interfaceId": interfaceId}, {reload: true});
        }

        $scope.pageQuery($stateParams.groupid, $scope.currentPage, $scope.itempersize);

        interfaceService.treeQuery().then(function (response) {

            var treedata = [{
                'label': '全部',
                'data': {
                    'id': ''
                },
                children: [],
                'onSelect': tree_selected
            }];

            angular.forEach(response.data, function (value, key) {

                this.push({
                    'label': value.name,
                    'data': value,
                    'onSelect': tree_selected
                });
            }, treedata[0].children);

            $scope.treedata = treedata;
        });

    });

angular.module('sbAdminApp').controller('interCardCtrl', function ($scope, $modalInstance, interfaceService, $log, $location, interfaceId, interfaceName) {
    // $scope.group = '';

    interfaceService.findthreshold(interfaceId, interfaceName).then(function (response) {
        $scope.warnthreshold = response.data;
    });

    $scope.save = function () {
        var interfaceId = $scope.warnthreshold.interfaceId;
        var threshold = $.trim($scope.warnthreshold.threshold);
        var iswarn = $scope.warnthreshold.isWarn;
        var avgElapsed = $.trim($scope.warnthreshold.avgElapsed);
        var isElapsed = $scope.warnthreshold.isElapsed;
        var intervalTime = $.trim($scope.warnthreshold.intervalTime);
        if (threshold == "" || threshold == null) {
            alert("阈值不能为空!");
            return false;
        }

        if (!/^-?\d+$/.test(threshold)) {
            alert("阈值请输入整数!");
            return false;
        }

        if (iswarn == "" || iswarn == null) {
            alert("是否通知未选择!");
            return false;
        }
        if (!/^\d+$/.test(avgElapsed)) {
            alert("平均耗值请输入不小于0的整数!");
            return false;
        }
        if (avgElapsed <= 0) {
            alert("平均耗值请输入不小于0的整数!");
            return false;
        }
        if (isElapsed == "" || isElapsed == null) {
            alert("是否告警未选择!");
            return false;
        }

        if (!/^[1-9]*[1-9][0-9]*$/.test(intervalTime)) {
            alert("统计频率请输入不小于5的整数!");
            return false;
        }
        if (intervalTime < 5) {
            alert("统计频率请输入不小于5整数!");
            return false;
        }
        interfaceService.saveWarnthreshold(interfaceId, threshold, iswarn, avgElapsed, isElapsed, intervalTime).then(function () {
            $modalInstance.close();
        }, function () {
        });
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});
angular.module('sbAdminApp').controller('interAddCardCtrl', function ($scope, $modalInstance, interfaceService, $log, $location, groupid, intergroupid,interfaceId,flag) {
	if(flag=="1"){//修改
		groupid = intergroupid;
	}
    interfaceService.queryGroup(groupid, interfaceId).then(function (response) {

        $scope.grouplist = response.data["groups"];
        $scope.groupId=groupid;
        $scope.flag=flag;
        if (interfaceId != "") {
            $scope.interfaceName = response.data["esfface"].interfacename;
        }
    });
    $scope.save = function () {
        var interfacename = $scope.interfaceName;
        var groupId = $scope.groupId;
        if (groupId == null || groupId == undefined) {
            alert("请选择组!");
            return false;
        }

        if (interfacename == null || interfacename == undefined || interfacename == '') {
            alert("接口名不能为空。");
            $("#interfaceName").focus();
            return false;
        }

        interfaceService.addInterface(groupId, interfaceId, interfacename).then(function () {
            $modalInstance.close();
        }, function () {
        });
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});